#!/bin/bash
if [ ! -f "./first_master_node.log" ];then
  long_k8s_version={{ kubernetes_version }}
  short_k8s_version=${long_k8s_version#*v}
  current_k8s_server_version=`kubectl version |grep "Server Version" |awk '{print $5}' |awk -F':' '{print $2}' |awk -F'"' '{print $2}'`
  current_k8s_client_version=`kubectl version |grep "Client Version" |awk '{print $5}' |awk -F':' '{print $2}' |awk -F'"' '{print $2}'`
  function version_gt() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"; }
  function version_lt() { test "$(echo "$@" | tr " " "\n" | sort -V -r | head -n 1)" != "$1"; }
  if (version_lt ${current_k8s_server_version} ${long_k8s_version}) || (version_lt ${current_k8s_client_version} ${long_k8s_version}); then
    apt-get update
    apt-get install -y kubeadm --disableexcludes=kubernetes
    kubectl drain `hostname`
    kubeadm upgrade node --certificate-renewal=false -y
    apt-get install -y kubelet kubectl --disableexcludes=kubernetes
    systemctl daemon-reload
    systemctl restart kubelet
    kubectl uncordon `hostname`
  else
    echo "current version is not lower than ${long_k8s_version}"
  fi
else
  echo 'This is the first master node.'
fi
rm -f ./first_master_node.log
